<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companName">
        <el-input
          v-model="queryParams.companName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="城市" prop="province">
        <el-select v-model="myprovince" placeholder="请选择省" @change="handleChangesheng"
                   @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in myArea"
            :key="item.value"
            :label="item.label"
            :value="item">
          </el-option>
        </el-select>
        <el-select v-model="mycity" placeholder="请选择市" @change="handleChangeshi">
          <el-option
            v-for="item in mycityQuery"
            :key="item.value"
            :label="item.label"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['moon:company:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['moon:company:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['moon:company:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公司名称" align="center" prop="companName" show-overflow-tooltip width="300"/>
      <el-table-column label="地区" align="center" prop="province" width="120">
        <template slot-scope="scope">
          {{ scope.row.province }}{{ scope.row.city }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="市" align="center" prop="city"/>-->
      <el-table-column label="公司评分" align="center" prop="companyRating">
        <template slot-scope="scope">
          {{ scope.row.companyRating }}分
        </template>
      </el-table-column>
      <el-table-column label="工资情况" align="center" prop="wage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wage" :value="scope.row.wage"/>
        </template>
      </el-table-column>
      <el-table-column label="社保情况" align="center" prop="socialSecurity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.social_security" :value="scope.row.socialSecurity"/>
        </template>
      </el-table-column>
      <el-table-column label="公积金情况" align="center" prop="reserveFund">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reserve_fund" :value="scope.row.reserveFund"/>
        </template>
      </el-table-column>
      <el-table-column label="工作制度" align="center" prop="weekendsClosed">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.weekends_closed" :value="scope.row.weekendsClosed"/>
        </template>
      </el-table-column>
      <el-table-column label="加班情况" align="center" prop="overtimesSituation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.overtimes_situation" :value="scope.row.overtimesSituation"/>
        </template>
      </el-table-column>
      <el-table-column label="首次发布时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['moon:company:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['moon:company:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companName">
              <el-input v-model="form.companName" placeholder="请输入公司名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--          <el-form-item label="公司曾用名" prop="bcompanyName">-->
            <!--            <el-input v-model="form.bcompanyName" placeholder="请输入公司曾用名" />-->
            <!--          </el-form-item>-->
            <el-form-item label="公司评分" prop="companyRating">
              <el-input v-model="form.companyRating" placeholder="请输入公司评分"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <!--                      <el-form-item label="公司简介" prop="content">-->
            <!--                        <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />-->
            <!--                      </el-form-item>-->

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="省份" prop="province">
              <el-select style="width: 100%" v-model="myprovince" placeholder="请选择省" @change="handleChangesheng"
                         @keyup.enter.native="handleQuery">
                <el-option
                  v-for="item in myArea"
                  :key="item.value"
                  :label="item.label"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市" prop="city">
              <el-select style="width: 100%" v-model="mycity" placeholder="请选择市" @change="handleChangeshi">
                <el-option
                  v-for="item in mycityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="工资发放情况" prop="wage">
              <el-select style="width: 100%" v-model="form.wage" placeholder="请选择工资发放情况">
                <el-option
                  v-for="dict in dict.type.wage"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社保情况" prop="socialSecurity">
              <el-select v-model="form.socialSecurity" style="width: 100%" placeholder="请选择社保情况">
                <el-option
                  v-for="dict in dict.type.social_security"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公积金情况" prop="reserveFund">
              <el-select v-model="form.reserveFund" style="width: 100%" placeholder="请选择公积金情况">
                <el-option
                  v-for="dict in dict.type.reserve_fund"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作制度" prop="weekendsClosed">
              <el-select style="width: 100%" v-model="form.weekendsClosed" placeholder="请选择工作休息制度">
                <el-option
                  v-for="dict in dict.type.weekends_closed"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="加班情况" prop="overtimesSituation">
              <el-select style="width: 100%" v-model="form.overtimesSituation" placeholder="请选择加班情况">
                <el-option
                  v-for="dict in dict.type.overtimes_situation"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>

        <!--                <el-form-item label="详情描述" prop="detailedDescription">-->
        <!--                  <el-input v-model="form.detailedDescription" type="textarea" placeholder="请输入内容" />-->
        <!--                </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCompany, getCompany, delCompany, addCompany, updateCompany} from "@/api/moon/company";
import {address} from "@/api/moon/reviews";

export default {
  name: "Company",
  dicts: ['reserve_fund', 'social_security', 'weekends_closed', 'overtimes_situation', 'wage'],
  data() {
    return {
      province: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companName: null,
        city: null,
        province: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      myArea: [],
      mycityQuery: [],
      myareaQuery: [],
      myprovince: "",
      mycity: '',
      myarea: "",
    };
  },
  created() {
    this.getList();
    this.getArr()
  },
  watch: {
    myprovince: function (newValue) {
      this.mycityQuery = []
      this.myareaQuery = []
      // console.log(this.myArea)
      this.myArea.forEach((item) => {
        if (newValue.value === item.value) {
          this.mycityQuery = item.children
        }
      })
    },
  },
  methods: {
    // 获取省市接口
    getArr() {
      address().then(res => {
        this.myArea = res.data
      })
    },
    // element
    handleChangesheng(value) {
      this.province = value.label
      this.provinceId = value.value
      this.queryParams.provinceId = this.provinceId
      this.form.province = this.province
      this.form.provinceId = this.provinceId
      // console.log(this.province)
    },
    handleChangeshi(value) {
      this.cityId = value.value
      this.queryParams.cityId = this.cityId
      this.form.city = value.label
      this.form.cityId = this.cityId
    },
    tableRowClassName({row, rowIndex}) {
      return "background: white";
    },

    /** 查询公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.mycity = null;
      this.myprovince = null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.cityId = null
      this.provinceId = null
      this.form = {
        id: null,
        userId: null,
        content: null,
        imgUrl: null,
        companName: null,
        bcompanyName: null,
        companyRating: null,
        detailedDescription: null,
        createTime: null,
        cityId: null,
        city: null,
        province: null,
        provinceId: null,
        wage: null,
        socialSecurity: null,
        reserveFund: null,
        weekendsClosed: null,
        overtimesSituation: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.cityId = null
      this.queryParams.provinceId = null
      this.mycity = null
      this.myprovince = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.queryParams.cityId = null
      this.queryParams.provinceId = null
      this.mycity = null
      this.myprovince = null
      this.reset();
      this.open = true;
      this.title = "添加公司";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        this.myprovince = this.form.province
        this.mycity = this.form.city


        this.open = true;
        this.title = "修改公司";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.queryParams.cityId = null
              this.queryParams.provinceId = null
              this.mycity = null
              this.myprovince = null
              this.cityId = null
              this.provinceId = null

              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.queryParams.cityId = null
              this.queryParams.provinceId = null
              this.mycity = null
              this.myprovince = null
              this.cityId = null
              this.provinceId = null
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公司编号为"' + ids + '"的数据项？').then(function () {
        return delCompany(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('moon/company/export', {
        ...this.queryParams
      }, `company_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
